package com.loongtech.bi.manager.business;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.pay.EntityFirstPayUser;
import com.loongtech.bi.manager.pay.TotalCashManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ReconciliationBusinessChannelManager extends ManagerQueryCacheBase<EntityFirstPayUser> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private TotalCashManager totalCashManager;
	@Autowired
	private AddcashBalanceManager addcashBalanceManager;
	
	//获得汇总数据
	public List<BusinessDataChannel> getBusinessSumData(Date dateStart, Date dateEnd){
		//web 直充
		String webzhichong = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.WEBZHICHONG).keySet().toArray()[1];
		//游戏内充值
		String youxineichongzhi = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.YOUXINEICHONGZHI).keySet().toArray()[1];
		
		List<BusinessDataChannel> result = new ArrayList<BusinessDataChannel>();
		BusinessDataChannel businessSumData = new BusinessDataChannel();
		//充值list
		List<Object[]> totalCashList = totalCashManager.getSumTotalCashByAllKind(dateStart, dateEnd, "0", "0", "0");
		businessSumData.setAddcash(UIHelper.getYuan(NumberUtil.parseLong(totalCashList.get(0))));
		//astore
		List<Object[]> astoreCashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, "0", "1", "0", youxineichongzhi);
		businessSumData.setAstoreAddcash(UIHelper.getYuan(NumberUtil.parseLong(astoreCashList.get(0))));
		//tstore
		List<Object[]> tstoreCashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, "0", "2", "0", youxineichongzhi);
		businessSumData.setTstoreAddcash(UIHelper.getYuan(NumberUtil.parseLong(tstoreCashList.get(0))));
		//nstore
		List<Object[]> nstoreCashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, "0", "3", "0", youxineichongzhi);
		businessSumData.setNstoreAddcash(UIHelper.getYuan(NumberUtil.parseLong(nstoreCashList.get(0))));
		//gstore
		List<Object[]> gstoreCashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, "0", "4", "0", youxineichongzhi);
		businessSumData.setGstoreAddcash(UIHelper.getYuan(NumberUtil.parseLong(gstoreCashList.get(0))));
		//web 直充
		List<Object[]> webAddcashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, "0", "0", "0", webzhichong);
		businessSumData.setWebAddcash(UIHelper.getYuan(NumberUtil.parseLong(webAddcashList.get(0))));
		
		//订单数总和
		List<Object[]> totalOrderNumList = addcashBalanceManager.getSumOrderNumByAllKind(dateStart, dateEnd, "0", "0", "0");
		businessSumData.setOdernum(NumberUtil.parseInt(totalOrderNumList.get(0)));
		//astore 订单
		List<Object[]> astoreOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, "0", "1", "0",youxineichongzhi);
		businessSumData.setAstoreOdernum(NumberUtil.parseInt(astoreOdernumList.get(0)));
		//tstore 订单
		List<Object[]> tstoreOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, "0", "2", "0",youxineichongzhi);
		businessSumData.setTstoreOdernum(NumberUtil.parseInt(tstoreOdernumList.get(0)));
		//nstore 订单
		List<Object[]> nstoreOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, "0", "3", "0",youxineichongzhi);
		businessSumData.setNstoreOdernum(NumberUtil.parseInt(nstoreOdernumList.get(0)));
		//gstore 订单
		List<Object[]> gstoreOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, "0", "4", "0",youxineichongzhi);
		businessSumData.setGstoreOdernum(NumberUtil.parseInt(gstoreOdernumList.get(0)));
		//web 订单
		List<Object[]> webOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, "0", "0", "0",webzhichong);
		businessSumData.setWebOdernum(NumberUtil.parseInt(webOdernumList.get(0)));
		
		//加入
		result.add(businessSumData);
		return result;
	}
	
	//获得详细数据
	public List<BusinessDataChannel> getBusinessDetailData(Date dateStart, Date dateEnd){
		//web 直充
		String webzhichong = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.WEBZHICHONG).keySet().toArray()[1];
		//游戏内充值
		String youxineichongzhi = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.YOUXINEICHONGZHI).keySet().toArray()[1];
				
		List<BusinessDataChannel> result = new ArrayList<BusinessDataChannel>();
		//map
		Map<String,BusinessDataChannel> map = new HashMap<String,BusinessDataChannel>();
		//all 充值
		List<Object[]> addcashList = totalCashManager.getTotalCashByAllKind(dateStart, dateEnd, "0", "0", "0");
		
		for(Object[] o : addcashList) {
			BusinessDataChannel businessData = new BusinessDataChannel();
			businessData.setDay(o[0].toString());
			businessData.setAddcash(businessData.getAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
			
			//map 存入
			map.put(o[0].toString(), businessData);
		}
		
		//astore充值
		List<Object[]> astoreAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, "0", "1", "0",youxineichongzhi);
		for(Object[] o : astoreAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setAstoreAddcash(businessData.getAstoreAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		
		//tstore充值
		List<Object[]> tstoreAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, "0", "2", "0",youxineichongzhi);
		for(Object[] o : tstoreAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setTstoreAddcash(businessData.getTstoreAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//nstore充值
		List<Object[]> nstoreAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, "0", "3", "0",youxineichongzhi);
		for(Object[] o : nstoreAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setNstoreAddcash(businessData.getNstoreAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//gstore充值
		List<Object[]> gstoreAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, "0", "4", "0",youxineichongzhi);
		for(Object[] o : gstoreAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setGstoreAddcash(businessData.getGstoreAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		
		//web充值
		List<Object[]> webAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, "0", "0", "0",webzhichong);
		for(Object[] o : webAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setWebAddcash(businessData.getWebAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//odernum
		List<Object[]> odernumList = addcashBalanceManager.getOrderNumByAllKindByDay(dateStart, dateEnd, "0", "0", "0");
		for(Object[] o : odernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setOdernum(businessData.getOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//astore
		List<Object[]> astoreOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, "0", "1", "0",youxineichongzhi);
		for(Object[] o : astoreOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setAstoreOdernum(businessData.getAstoreOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//tstore
		List<Object[]> tstoreOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, "0", "2", "0",youxineichongzhi);
		for(Object[] o : tstoreOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setTstoreOdernum(businessData.getTstoreOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//nstore
		List<Object[]> nstoreOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, "0", "3", "0",youxineichongzhi);
		for(Object[] o : nstoreOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setNstoreOdernum(businessData.getNstoreOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//gstore
		List<Object[]> gstoreOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, "0", "4", "0",youxineichongzhi);
		for(Object[] o : gstoreOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setGstoreOdernum(businessData.getGstoreOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//webOdernum
		List<Object[]> webOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, "0", "0", "0",webzhichong);
		for(Object[] o : webOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setWebOdernum(businessData.getWebOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : addcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }
			BusinessDataChannel businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				result.add(businessData);
			}
		}
		
		return result;
	}
	
	//对账汇总数据
	public class BusinessDataChannel {
		private String day;
		private long addcash; //充值
		private long astoreAddcash; //astore游戏内充值
		private long tstoreAddcash; //tstore游戏内充值
		private long nstoreAddcash; //nstore游戏内充值
		private long gstoreAddcash; //gstore游戏内充值
		private long webAddcash; //web直充
		private int odernum; //订单号
		private int astoreOdernum; //astore游戏内订单号
		private int tstoreOdernum; //tstore游戏内订单号
		private int nstoreOdernum; //nstore游戏内订单号
		private int gstoreOdernum; //gstore游戏内订单号
		private int webOdernum; //web直充订单号
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public long getAddcash() {
			return addcash;
		}
		public void setAddcash(long addcash) {
			this.addcash = addcash;
		}
		public long getAstoreAddcash() {
			return astoreAddcash;
		}
		public void setAstoreAddcash(long astoreAddcash) {
			this.astoreAddcash = astoreAddcash;
		}
		public long getTstoreAddcash() {
			return tstoreAddcash;
		}
		public void setTstoreAddcash(long tstoreAddcash) {
			this.tstoreAddcash = tstoreAddcash;
		}
		public long getNstoreAddcash() {
			return nstoreAddcash;
		}
		public void setNstoreAddcash(long nstoreAddcash) {
			this.nstoreAddcash = nstoreAddcash;
		}
		public long getGstoreAddcash() {
			return gstoreAddcash;
		}
		public void setGstoreAddcash(long gstoreAddcash) {
			this.gstoreAddcash = gstoreAddcash;
		}
		public long getWebAddcash() {
			return webAddcash;
		}
		public void setWebAddcash(long webAddcash) {
			this.webAddcash = webAddcash;
		}
		public int getOdernum() {
			return odernum;
		}
		public void setOdernum(int odernum) {
			this.odernum = odernum;
		}
		public int getAstoreOdernum() {
			return astoreOdernum;
		}
		public void setAstoreOdernum(int astoreOdernum) {
			this.astoreOdernum = astoreOdernum;
		}
		public int getTstoreOdernum() {
			return tstoreOdernum;
		}
		public void setTstoreOdernum(int tstoreOdernum) {
			this.tstoreOdernum = tstoreOdernum;
		}
		public int getNstoreOdernum() {
			return nstoreOdernum;
		}
		public void setNstoreOdernum(int nstoreOdernum) {
			this.nstoreOdernum = nstoreOdernum;
		}
		public int getGstoreOdernum() {
			return gstoreOdernum;
		}
		public void setGstoreOdernum(int gstoreOdernum) {
			this.gstoreOdernum = gstoreOdernum;
		}
		public int getWebOdernum() {
			return webOdernum;
		}
		public void setWebOdernum(int webOdernum) {
			this.webOdernum = webOdernum;
		}
	}
}
